home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / ui / layout / example / AppletButton.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  5.9 KB  |  172 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18. import java.util.*;
  19. import java.applet.Applet;
  20.  
  21. public class AppletButton extends Applet implements Runnable {
  22.     int frameNumber = 1;
  23.     String windowClass;
  24.     String buttonText;
  25.     String windowTitle;
  26.     int requestedWidth = 0;
  27.     int requestedHeight = 0;
  28.     Button button;
  29.     Thread windowThread;
  30.     Label label;
  31.     boolean pleaseCreate = false;
  32.  
  33.     public void init() {
  34.         windowClass = getParameter("WINDOWCLASS");
  35.         if (windowClass == null) {
  36.             windowClass = "TestWindow";
  37.         }
  38.  
  39.         buttonText = getParameter("BUTTONTEXT");
  40.         if (buttonText == null) {
  41.             buttonText = "Click here to bring up a " + windowClass;
  42.         }
  43.  
  44.         windowTitle = getParameter("WINDOWTITLE");
  45.         if (windowTitle == null) {
  46.             windowTitle = windowClass;
  47.         }
  48.  
  49.         String windowWidthString = getParameter("WINDOWWIDTH");
  50.         if (windowWidthString != null) {
  51.             try {
  52.                 requestedWidth = Integer.parseInt(windowWidthString);
  53.             } catch (NumberFormatException e) {
  54.                 //Use default width.
  55.             }
  56.         }
  57.  
  58.         String windowHeightString = getParameter("WINDOWHEIGHT");
  59.         if (windowHeightString != null) {
  60.             try {
  61.                 requestedHeight = Integer.parseInt(windowHeightString);
  62.             } catch (NumberFormatException e) {
  63.                 //Use default height.
  64.             }
  65.         }
  66.  
  67.         setLayout(new GridLayout(2,0));
  68.         add(button = new Button(buttonText));
  69.         button.setFont(new Font("Helvetica", Font.PLAIN, 14));
  70.  
  71.         add(label = new Label("", Label.CENTER));
  72.     }
  73.  
  74.     public void start() {
  75.         if (windowThread == null) {
  76.             windowThread = new Thread(this, "Bringing Up " + windowClass);
  77.             windowThread.start();
  78.         }
  79.     }
  80.  
  81.     public synchronized void run() {
  82.         Class windowClassObject = null;
  83.         Class tmp = null;
  84.         String name = null;
  85.         
  86.         // Make sure the window class exists and is really a Frame.
  87.         // This has the added benefit of pre-loading the class,
  88.         // which makes it much quicker for the first window to come up.
  89.         try {
  90.             windowClassObject = Class.forName(windowClass);
  91.         } catch (Exception e) {
  92.             // The specified class isn't anywhere that we can find.
  93.             label.setText("Can't create window: Couldn't find class "
  94.                               + windowClass);
  95.             button.disable();
  96.             return;
  97.         }
  98.  
  99.         // Find out whether the class is a Frame.
  100.         for (tmp = windowClassObject, name = tmp.getName();
  101.              !( name.equals("java.lang.Object") ||
  102.                 name.equals("java.awt.Frame") ); ) {
  103.             tmp = tmp.getSuperclass();
  104.             name = tmp.getName();
  105.         }
  106.         if ((name == null) || name.equals("java.lang.Object")) {
  107.             //We can't run; ERROR; print status, never bring up window
  108.             label.setText("Can't create window: "
  109.                               + windowClass +
  110.                           " isn't a Frame subclass.");
  111.             button.disable();
  112.             return;
  113.         } else if (name.equals("java.awt.Frame")) { 
  114.             //Everything's OK. Wait until we're asked to create a window.
  115.             while (windowThread != null) {
  116.                 while (pleaseCreate == false) {
  117.                     try {
  118.                         wait();
  119.                     } catch (InterruptedException e) {
  120.                     }
  121.                 }
  122.  
  123.                 //We've been asked to bring up a window.
  124.                 pleaseCreate = false;
  125.                 Frame window = null;
  126.                 try {
  127.                     window = (Frame)windowClassObject.newInstance();
  128.                 } catch (Exception e) {
  129.                     label.setText("Couldn't create instance of class "
  130.                                   + windowClass);
  131.                     button.disable();
  132.                     return;
  133.                 }
  134.                 if (frameNumber == 1) {
  135.                     window.setTitle(windowTitle);
  136.                 } else {
  137.                     window.setTitle(windowTitle + ": " + frameNumber);
  138.                 }
  139.                 frameNumber++;
  140.  
  141.                 //Set the window's size.
  142.                 window.pack();
  143.                 if ((requestedWidth > 0) | (requestedHeight > 0)) {
  144.                     window.resize(Math.max(requestedWidth,
  145.                                            window.size().width),
  146.                                   Math.max(requestedHeight,
  147.                                            window.size().height));
  148.                 }
  149.  
  150.                 window.show();
  151.                 label.setText("");
  152.             }
  153.         }
  154.     }
  155.                 
  156.     public synchronized boolean action(Event event, Object what) {
  157.         if (event.target instanceof Button) {
  158.             //signal the window thread to build a window
  159.             label.setText("Please wait while the window comes up...");
  160.             pleaseCreate = true;
  161.             notify();
  162.         } 
  163.         return true;
  164.     }
  165. }
  166.  
  167. class TestWindow extends Frame {
  168.     public TestWindow() {
  169.         resize(300, 300);
  170.     }
  171. }
  172.